package com.lang.exception;

import java.lang.reflect.InvocationTargetException;

/**
 * 反射操作异常
 */
public class ReflectException extends RuntimeException {

    public ReflectException(String message) {
        super("反射操作异常:" + message);
    }

    public ReflectException(String message, Throwable cause) {
        super("反射操作异常:" + message, cause);
    }

    public ReflectException(Throwable cause) {
        Class<? extends Throwable> exceptionType = cause.getClass();
        if (NoSuchMethodException.class.isAssignableFrom(exceptionType)) {
            throw new ReflectException("找不到指定的方法", cause);
        } else if (IllegalAccessException.class.isAssignableFrom(exceptionType)) {
            throw new ReflectException("反射访问权限异常", cause);
        } else if (InvocationTargetException.class.isAssignableFrom(exceptionType)) {
            throw new ReflectException("执行反射方法对象操作异常", cause);
        } else {
            throw new ReflectException("未知异常", cause);
        }
    }
}
